package com.syh.jn.scm.domain.downstream.entity.system;

import cn.redscarf.commons.util.bean.ExtBeanUtils;
import cn.redscarf.commons.util.other.ExtAssert;
import cn.redscarf.commons.spring.SpringContextUtils;
import cn.redscarf.commons.util.validator.ValidationUtils;
import cn.redscarf.commons.wrapper.Restrictions;
import cn.redscarf.commons.wrapper.Wrapper;
import com.syh.core.ddd.domain.AbstractSimpleAggregate;
import com.syh.jn.scm.domain.downstream.repository.system.DownAccountReadRepository;
import com.syh.jn.scm.domain.supplier.entity.base.downstream.Downstream;
import com.syh.jn.scm.domain.supplier.repository.base.downstream.DownstreamReadRepository;
import com.syh.jn.scm.dto.downstream.system.downAccount.DownAccountMto;
import com.syh.jn.scm.dto.downstream.system.downAccount.DownAccountSto;
import com.syh.jn.scm.exception.BusinessException;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * 运营账号管理
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class DownAccount extends AbstractSimpleAggregate<Long> {
	/**
	 * 登录账号
	 */
	private String loginAccount;
	/**
	 * 登录密码
	 */
	private String loginPassword;
	/**
	 * 手机账号id
	 */
	private Long loginPhoneId;
	/**
	 * 数据归属公司ID
	 */
	private Long businessEntityId;
	/**
	 * 姓名
	 */
	private String name;
	/**
	 * 电话
	 */
	private String telephone;
	/**
	 * 终端系统类型
	 */
	private String terminalType;
	/**
	 * 角色id
	 */
	private Long roleId;
	/**
	 * 是否管理员 1:是
	 */
	private Short admin;
	/**
	 * 使用情况:-1冻结, 1启用
	 */
	private Short enable;

	public DownAccount() {
	}

	public DownAccount(Long id) {
		this.id = id;
	}

	public DownAccount(DownAccountSto sto) {
		ExtAssert.notNull(sto, "对象为空");
		ValidationUtils.validate(sto);

		DownstreamReadRepository readRepository = (DownstreamReadRepository) SpringContextUtils.getBean("downstreamRepositoryImpl");
		DownAccountReadRepository accountReadRepository = (DownAccountReadRepository) SpringContextUtils.getBean(DownAccountReadRepository.class);
		Downstream downstream = readRepository.getOne(Wrapper.build().append(Restrictions.eq("id",sto.getBusinessEntityId())));
		//生成带code的账号
		StringBuffer downstreamAccount = new StringBuffer(sto.getLoginAccount()).append("@").append(downstream.getCode());
		sto.setLoginAccount(downstreamAccount.toString());
		//检查账号是否已存在
		Long count = accountReadRepository.getCountByAccountName(sto.getLoginAccount());
		if (count.intValue() > 0) {
			throw new BusinessException("帐号已经存在,请更换帐号名重试");
		}

		this.terminalType = "DOWNSTREAM";
		this.createTimeStamp = System.currentTimeMillis();
		this.enable = Short.valueOf("1");
		ExtBeanUtils.copyProperties(sto, this, false);
	}

	public void modify(DownAccountMto mto) {
		ExtAssert.notNull(mto, "对象为空");
		ValidationUtils.validate(mto);

		DownstreamReadRepository readRepository = (DownstreamReadRepository) SpringContextUtils.getBean(DownstreamReadRepository.class);
		DownAccountReadRepository accountReadRepository = (DownAccountReadRepository) SpringContextUtils.getBean(DownAccountReadRepository.class);
		Downstream downstream = readRepository.getOne(Wrapper.build().append(Restrictions.eq("id",mto.getBusinessEntityId())));
		//重置密码时获取数据库的账号需要预处理
		String suffix = "@"+downstream.getCode();
		if(mto.getLoginAccount().indexOf(suffix) > 0 ){
			mto.setLoginAccount(mto.getLoginAccount().split(suffix)[0]);
		}
		//生成带code的账号
		StringBuffer downstreamAccount = new StringBuffer(mto.getLoginAccount()).append(suffix);
		mto.setLoginAccount(downstreamAccount.toString());
		//检查账号是否已存在
		Long count = accountReadRepository.getCountByAccountName(mto.getLoginAccount(),mto.getId());
		if (count.intValue() > 0) {
			throw new BusinessException("帐号已经存在,请更换帐号名重试");
		}

		if (this.enable.equals(Short.valueOf("-1"))) {//冻结将所属角色取消
			this.roleId = null;
		}
		this.lastUpdateTimeStamp = System.currentTimeMillis();
		ExtBeanUtils.copyProperties(mto, this, false);
	}

	public void bindingLoginPhone(Long phoneId) {
		if (this.loginPhoneId != null) {
			throw new BusinessException(this.loginAccount+"已绑定手机,请先解绑原手机再重新绑定");
		}
		this.loginPhoneId = phoneId;
	}

	public void untyingLoginPhone() {
		this.loginPhoneId = null;
	}

	public void changeEnable(Short enable) {
		this.enable = enable;
		/*if (this.enable.equals(Short.valueOf("-1"))) {//冻结将所属角色取消
			this.roleId = null;
		}
		this.lastUpdateTimeStamp = System.currentTimeMillis();*/
	}

	public void changeRole(Long roleId) {
		ExtAssert.notNull(roleId, "id为空");
		this.roleId = roleId;
		this.lastUpdateTimeStamp = System.currentTimeMillis();
	}

	public void destory() {
		if(this.admin != null && this.admin.equals(Short.valueOf("1")))
			throw new BusinessException("主账号不可删除！");
	}
}
